package utils;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import aw.Agent;

public class ReadXMLFile {
	
	public ReadXMLFile(){}
	
	public World createMap() {
		
		World world = new World();

		try {
			File fXmlFile = new File("Map.xml");
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(fXmlFile);

			doc.getDocumentElement().normalize();

			System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
			
			NodeList nList = doc.getElementsByTagName("room");

			for (int temp = 0; temp < nList.getLength(); temp++) {

				Node nNode = nList.item(temp);
				
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {

					Element eElement = (Element) nNode;
					int roomId = Integer.parseInt(eElement.getAttribute("id"));
					NamedNodeMap firstPos = eElement.getElementsByTagName("firstPosition").item(0).getAttributes();
					NamedNodeMap lastPos = eElement.getElementsByTagName("lastPosition").item(0).getAttributes();
					NodeList acessories = eElement.getElementsByTagName("object");
					NodeList doors = eElement.getElementsByTagName("door");
					ArrayList<Acessory> acessoriesArray = new ArrayList<Acessory>();
					ArrayList<Door> doorsArray = new ArrayList<Door>();
					
					int firstPosX = Integer.parseInt(firstPos.getNamedItem("x").getNodeValue());
					int firstPosY = Integer.parseInt(firstPos.getNamedItem("y").getNodeValue());
					
					int lastPosX = Integer.parseInt(lastPos.getNamedItem("x").getNodeValue());
					int lastPosY = Integer.parseInt(lastPos.getNamedItem("y").getNodeValue());
					
					for (int i = 0; i< acessories.getLength(); i++) {
						Element auxAcessory = (Element) acessories.item(i);
						String id = auxAcessory.getAttribute("id");
						int posX = Integer.parseInt(auxAcessory.getAttribute("x"));
						int posY = Integer.parseInt(auxAcessory.getAttribute("y"));
						System.out.println("objecto: " + id + " " + posX + " " + posY);
						Acessory ac = new Acessory(id, posX, posY);
						acessoriesArray.add(ac);
					}
					
					for (int j = 0; j< doors.getLength(); j++) {
						Element auxDoor = (Element) doors.item(j);
						int posX = Integer.parseInt(auxDoor.getAttribute("x"));
						int posY = Integer.parseInt(auxDoor.getAttribute("y"));
						int nextRoom = Integer.parseInt(auxDoor.getAttribute("nextRoom"));
						String state = auxDoor.getAttribute("state");
						System.out.println("door: " + posX + " " + posY + " " + nextRoom + " " + state);
						Door d = new Door(posX, posY, nextRoom, state);
						doorsArray.add(d);
					}
					
					Room room = new Room(roomId, firstPosX, firstPosY, lastPosX, lastPosY, acessoriesArray, doorsArray);
					
					world.setRooms(roomId, room);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return world;
	}
	
	public void printMap(World world, ArrayList<Agent> agents){
		
		String line = "";
		
		System.out.println("######################");
		boolean jacabou = false;
		for(int i = 0; i<20; i++){
			
			if(i<10){
				Room r0 = world.getRoom(0);
				Room r1 = world.getRoom(1);
				line += "#";
				for(int j=0;j<10;j++){
					jacabou = false;
					ArrayList<Acessory> a0 = r0.getAcessories();
					for (Acessory acessory : a0) {
						if(acessory.getPosX()==j && acessory.getPosY()==i){
							jacabou = true;
							line += acessory.getId();
							break;
						}
					}
					if(jacabou) continue;
					for (Agent agent : agents) {
						int a = 1;
						String[] pos = agent.getPosition().split("[*]");
						int posX = Integer.parseInt(pos[0]);
						int posY = Integer.parseInt(pos[1]);
						if(posX==j && posY==i){
							jacabou = true;
							line += a;
							break;
						}
						a++;
					}
					if(jacabou) continue;
					line += ".";
				}
				line += "#";
				for(int j=10;j<20;j++){
					jacabou = false;
					ArrayList<Acessory> a1 = r1.getAcessories();
					for (Acessory acessory : a1) {
						if(acessory.getPosX()==j && acessory.getPosY()==i){
							jacabou = true;
							line += acessory.getId();
							break;
						}
					}
					if(jacabou) continue;
					for (Agent agent : agents) {
						int a = 1;
						String[] pos = agent.getPosition().split("[*]");
						int posX = Integer.parseInt(pos[0]);
						int posY = Integer.parseInt(pos[1]);
						if(posX==j && posY==i){
							jacabou = true;
							line += a;
							break;
						}
						a++;
					}
					if(jacabou) continue;
					line += ".";
				}
				line += "#";
			}else if (i==10){
				line = "######################";
			}else{
				Room r2 = world.getRoom(2);
				Room r3 = world.getRoom(3);
				line += "#";
				for(int j=0;j<10;j++){
					jacabou = false;
					ArrayList<Acessory> a2 = r2.getAcessories();
					for (Acessory acessory : a2) {
						if(acessory.getPosX()==j && acessory.getPosY()==i){
							jacabou = true;
							line += acessory.getId();
							break;
						}
					}
					if(jacabou) continue;
					for (Agent agent : agents) {
						int a = 1;
						String[] pos = agent.getPosition().split("[*]");
						int posX = Integer.parseInt(pos[0]);
						int posY = Integer.parseInt(pos[1]);
						if(posX==j && posY==i){
							jacabou = true;
							line += a;
							break;
						}
						a++;
					}
					if(jacabou) continue;
					line += ".";
				}
				line += "#";
				for(int j=10;j<20;j++){
					jacabou = false;
					ArrayList<Acessory> a3 = r3.getAcessories();
					for (Acessory acessory : a3) {
						if(acessory.getPosX()==j && acessory.getPosY()==i){
							jacabou = true;
							line += acessory.getId();
							break;
						}
					}
					if(jacabou) continue;
					for (Agent agent : agents) {
						int a = 1;
						String[] pos = agent.getPosition().split("[*]");
						int posX = Integer.parseInt(pos[0]);
						int posY = Integer.parseInt(pos[1]);
						if(posX==j && posY==i){
							jacabou = true;
							line += a;
							break;
						}
						a++;
					}
					if(jacabou) continue;
					line += ".";
				}
				line += "#";
			}
			
			System.out.println(line);
			line="";
		}
		
		
		System.out.println("######################");
	}

}